package com.shujia.service;

import com.shujia.bean.User;
import com.shujia.dao.UserDaoInerace;
import com.shujia.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务层
 */

@Service
public class UserService implements UserServiceInterface {

    //查询用户名是否存在
    @Autowired
    UserDaoInerace userDao;


    public UserService(UserDaoInerace userDao) {
        this.userDao = userDao;
    }

    /**
     * 登录
     */

    public String login(String username, String password) {


        //调用dao层
        User user = userDao.queryUserByUsername(username);

        if (user == null) {
            return "用户不存在";
        }

        //判断密码是否正确
        String md5 = MD5Util.getMD5(password);

        if (!user.getPassword().equals(md5)) {

            return "密码不正确";
        }

        return "登录成功";
    }


    /**
     * 注册
     * 1、判断用户名是否存在
     * 2、判断两次密码是否一致
     * 3、插入数据
     * 对密码进行加密
     */

    public String register(String username, String paasword, String paasword1) {

        User user = userDao.queryUserByUsername(username);
        if (user != null) {
            return "用户名已存在";
        }

        if (!paasword.equals(paasword1)) {
            return "密码不一致";
        }

        //对密码进行md5加密

        String md5Password = MD5Util.getMD5(paasword);

        User user1 = new User(username, md5Password);

        userDao.insert(user1);

        return "注册成功";
    }

}
